package com.qiji.video.exception;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.qiji.video.util.Result;
import com.qiji.video.util.ResultCode;
import com.qiji.video.util.ResultUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕获
 */
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(com.qiji.video.exception.ServiceException.class)
    public Result handleService(com.qiji.video.exception.ServiceException e){
        System.err.println("Service层 :"+e.getMsg());
        return ResultUtil.error(e.getCode(),e.getMsg());
    }
    @ExceptionHandler(AuthenticationException.class)
    public Result handleAuthenticationException(AuthenticationException e){
        return ResultUtil.error(203,"该用户不存在");
    }
    @ExceptionHandler(IncorrectCredentialsException.class)
    public Result handleShiro(IncorrectCredentialsException e){
        return new Result(ResultCode.PASSWORD_ERROR);
    }
    @ExceptionHandler(Exception.class)
    public Result handleService(Exception e){
        return new Result(203,e.getMessage());
//        return new Result(503,"服务器内部错误");
    }
    @ExceptionHandler(TokenExpiredException.class)
    public Result handleTokenExpired(TokenExpiredException e){
        return new Result(205,e.getMessage());
    }

}
